from django.shortcuts import HttpResponse, render, redirect


def index(request):
    # 业务逻辑
    return HttpResponse('欢迎进入红浪漫!')


def home(request):
    return render(request, 'home.html')


# def login(request):
#     if request.method == 'GET':
#         # 返回登录页面
#         return render(request, 'login.html')
#     elif request.method == 'POST':
#         # 获取提交的数据 request.POST
#         username = request.POST.get('username')
#         password = request.POST.get('password')
#         # 对用户名和密码进行校验
#         if username == 'alex' and password == 'alexdsb':
#             # 校验成功 跳转到首页(重定向)
#             # return HttpResponse('欢迎进入红浪漫!')
#             return redirect('/index/')
#         else:
#             # 校验失败 重新登陆
#             return render(request, 'login.html')

from app01 import models


def login(request):
    if request.method == 'GET':
        # 返回登录页面

        return render(request, 'login.html')
    elif request.method == 'POST':
        # 获取提交的数据 request.POST
        user = request.POST.get('username')
        pwd = request.POST.get('password')
        # 对用户名和密码进行校验
        # ret = models.User.objects.get(username=user, ) # 找不到就报错  找到多个也报错
        ret = models.User.objects.filter(username=user,password=pwd )  # 对象列表

        if ret:
            # 校验成功 跳转到首页(重定向)
            # return HttpResponse('欢迎进入红浪漫!')
            return redirect('/index/')
        else:
            # 校验失败 重新登陆
            return render(request, 'login.html')
